/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.context;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.SchedulerException;
import util.www.jdbc.JdbcUtil;

/**
 * http://stackoverflow.com/questions/8184835/call-method-on-undeploy-from-a-java-web-application
 * @author Rdelacruz
 */
public class ServletContextListenerImpl 
implements ServletContextListener
{
    //Logger logger = Logger.getLogger(ServletContextListenerImpl.class.getName());

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("INICIANDO TOMCAT");
        try {            
            AppContext appContext = AppContext.getInstancia();
            appContext.inicializar();                        
        } catch (Exception ex) {
            Logger.getLogger(ServletContextListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("FINALIZANDO TOMCAT");
        try {            
            AppContext.getInstancia().finalizar();
            System.out.println("Desregistrando Drivers - inicio");            
            //Para evitar java.lang.OutOfMemoryError: PermGen space
            JdbcUtil.desregistrarDriversCargados(Logger.getLogger(ServletContextListenerImpl.class.getName()));
            System.out.println("Desregistrando Drivers - fin");
        } catch (SchedulerException ex) {
            Logger.getLogger(ServletContextListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
